// fb line x 分享
export const shareUtils = (type, url, hash, text) => {
    if (type === "fb") {
        window.open(`http://www.facebook.com/sharer.php?u=${encodeURIComponent(url)}${hash ? `&hashtag=${encodeURIComponent(hash)}` : ''}`);
    } else if (type === "line") {
        window.open(`https://social-plugins.line.me/lineit/share?url=${encodeURIComponent(url)}${text ? `&text=${encodeURIComponent(text)}` : ''}`);
    } else if (type === 'x') {
        window.open(`https://twitter.com/intent/tweet?url=${encodeURIComponent(url)}${hash ? `&hashtags=${encodeURIComponent(hash)}` : ''}${text ? `&text=${encodeURIComponent(text)}` : ''}`);
    }
}
// 复制链接
const copyLink = (url) => {
    try {
        if (navigator.clipboard && window.isSecureContext) {
            navigator.clipboard.writeText(url)
                .then(() => {
                    alert("複製成功")
                })
                .catch(() => {
                    alert("複製失敗 \n" + url)
                });
        } else {
            alert("複製失敗 \n" + url)
        }
    } catch (err) {
        console.error('Failed to copy: ', err);
        const textArea = document.createElement('textarea');
        textArea.value = url;
        document.body.appendChild(textArea);
        textArea.select();  // 选择文本
        document.execCommand('copy');  // 执行复制命令
        alert("複製成功")
        document.body.removeChild(textArea);  // 移除临时输入框
    }
}
// 生成dom图片
// 安装 npm install html2canvas --save
const html2Img = async (dom) => { // 传入dom元素, 下载生成图片 
    const scale = window.devicePixelRatio;  // 设备像素与CSS像素的比例，设置为1也可以，图片放大会模糊
    const width = dom.offsetWidth;
    const height = dom.offsetHeight;
    // backgroundColor: null png时必须要有不然会有白边 useCORS: true 允许跨域
    const ops = { backgroundColor: null, scale, width, height, useCORS: true };
    await html2canvas(dom, ops).then(function (canvas) {
        let imgData = canvas.toDataURL('image/png');
        let link = document.createElement('a');
        link.download = 'share.png';
        link.href = imgData;
        link.click();
    });
}